import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, Like, UpdateResult, Not, IsNull } from 'typeorm';
import { InternetCelebrity } from './internetCelebrity.entity';

@Injectable()
export class InternetCelebrityService {
  constructor(
    @InjectRepository(InternetCelebrity)
    private internetCelebrityRepository: Repository<InternetCelebrity>,
  ) {}

  /**
   * 根据平台id查询网红
   * @param account 
   * @returns 
   */
  findAll_ByAccount(account: string): Promise<InternetCelebrity[]> {
    return this.internetCelebrityRepository.query(`
      select distinct ic_group as id,account from internet_celebrity
      where LOWER(account) like LOWER(?) and ic_group is not null and account != ''
      limit 0,50
    `,[`%${account||''}%`])
  }

  /**
   * 查询平台下所有网红
   * @param platform
   * @param wx_user
   * @returns
   */
  async findAll_ByPlatform(platform: number, wx_user: string): Promise<InternetCelebrity[]> {
    return this.internetCelebrityRepository.query(`
      select * from internet_celebrity
      where platform = ? and wx_user = ? and is_group is null
    `,[platform, wx_user])
  }


  async findAll(skip: number, take: number, keyword: string, platformList: [], wx_user: string): Promise<{data:InternetCelebrity[],total:number}> {
    let where = []
    platformList.map(e => {
      where.push({
        'name':Like(`%${keyword}%`),
        platform:e,
        wx_user:wx_user
      })
    })
    if(where.length === 0){
      where.push({
        'name':Like(`%${keyword}%`),
        wx_user:wx_user
      })
    }
    let dataList = await this.internetCelebrityRepository.find({
      relations: ['platform_'],
      order:{create_time:'desc'},
      take:take,skip:skip,
      where:where
    });

    let totalCount = await this.internetCelebrityRepository.count({
      where:where
    })
    return {
      data:dataList,
      total:totalCount
    }
  }

  findAll_By_Platform(platform: number, wx_user: string): Promise<InternetCelebrity[]> {
    return this.internetCelebrityRepository.find({
      where:{
        platform:platform,
        wx_user:wx_user
      }
    });
  }

  findAll_ByName(name: string): Promise<InternetCelebrity[]> {
    return this.internetCelebrityRepository.find({
      where:{
        name:Like(`%${name}%`)
      }
    });
  }

  findOne_ById(id: number): Promise<InternetCelebrity> {
    return this.internetCelebrityRepository.findOne({where:{id}});
  }

  findOne_ByName(name: string): Promise<InternetCelebrity> {
    return this.internetCelebrityRepository.findOne({where:{name:name}});
  }

  async remove(id: number): Promise<void> {
    await this.internetCelebrityRepository.delete(id);
  }


  create(internetCelebrity: InternetCelebrity): Promise<InternetCelebrity> {
    return this.internetCelebrityRepository.save(internetCelebrity);
  }

  update(name: string, platform: number, id: number): Promise<UpdateResult> {
    return this.internetCelebrityRepository.update({id:id}, {name:name,platform:platform});
  }
}
